﻿from selenium import webdriver  # 用来驱动浏览器的
from selenium.webdriver import ActionChains  # 破解滑动验证码的时候用的 可以拖动图片
from selenium.webdriver.common.by import By  # 按照什么方式查找，By.ID,By.CSS_SELECTOR
from selenium.webdriver.common.keys import Keys  # 键盘按键操作
from selenium.webdriver.support import expected_conditions as EC  # 和下面WebDriverWait一起用的
from selenium.webdriver.support.wait import WebDriverWait  # 等待页面加载某些元素
import pycurl
#python main.py --url https://maoyan.com/films/celebrity/986135 --path g:/download2
import time
import argparse
import os
import wget
import certifi
if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('--url',type=str,nargs='+')
    parser.add_argument('--path',type=str,nargs='+')
    args = parser.parse_args()
    print(args)
    targeturl =args.url[0]
    targetpath =args.path[0]
    options = webdriver.ChromeOptions()

    prefs = {'profile.default_content_settings.popups': 0,
         'download.default_directory': targetpath}
    options.add_experimental_option('prefs', prefs)
    options.add_argument('--disable-gpu')
    options.add_argument('--ignore-certificate-errors')
    browser = webdriver.Chrome(chrome_options=options)
    browser.implicitly_wait(6) 

    browser.get(targeturl)
    time.sleep(3)

    more_btns=browser.find_elements_by_class_name('more')
    if more_btns:
        for b in more_btns :
            try:
                b.click()
            except Exception as e:
                print("error:"+str(e))
    
    time.sleep(3)

    #imgs=browser.find_elements_by_xpath("//div[@class='lg-thumb-item']//img")
    '''
    for img in imgs :
        imgurl=img.get_attribute('src')
        if imgurl:
            imgurl=imgurl.split("@")[0]
            print(imgurl)
            try:
                file_name =wget.filename_from_url(imgurl)
                returned_value  =os.system("curl -k -o "+os.path.join(targetpath, file_name)+" "+imgurl) 
                print('returned value:', returned_value) 
            except Exception as e:
                print("error:"+str(e))
    '''

    while True :
        print(len(browser.find_elements_by_class_name("lg-current")))
        imgs=browser.find_elements_by_class_name("lg-current")[0].find_elements_by_xpath("./div[@class='lg-img-wrap']/img")
        print(len(imgs))
        for img in imgs :
            imgurl=img.get_attribute('src')
            if imgurl:
                imgurl=imgurl.split("@")[0]
                print(imgurl)
                try:
                    file_name =wget.filename_from_url(imgurl)
                    returned_value  =os.system("curl -k -o "+os.path.join(targetpath, file_name)+" "+imgurl) 
                    print('returned value:', returned_value) 
                except Exception as e:
                    print("error:"+str(e))
        next_btn =browser.find_elements_by_class_name("lg-next")[0]
        next_btn_disabled =next_btn.get_attribute('disabled')
        print(next_btn_disabled)
        if next_btn_disabled != None :
            break
        else:
            next_btn.click()




    
   

    browser.quit()
